package cc.rengu.igas.channel.cups.core.service.trans;


import cc.rengu.igas.channel.cups.common.constant.CupsParamConstant;
import cc.rengu.igas.channel.cups.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.cups.core.model.CupsFieldRspBean;
import cc.rengu.igas.channel.cups.core.service.base.BasePayService;
import cc.rengu.igas.channel.cups.facade.request.AcctBalanceQueryRequest;
import cc.rengu.igas.channel.cups.facade.request.CupsACRequest;
import cc.rengu.igas.channel.cups.facade.request.EnchashmentRequest;
import cc.rengu.igas.channel.cups.facade.response.EnchashmentResponse;
import cc.rengu.igas.share.facade.bean.CupsField;
import cc.rengu.igas.share.facade.bean.CupsFieldBean;
import cc.rengu.igas.share.facade.bean.cups.CupsRspCodeEnum;
import cc.rengu.igas.share.facade.bean.cups.F57;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * 取现
 *
 * @author Administrator
 */
public class CupsEnchashmentService extends BasePayService<CupsACRequest, EnchashmentResponse> {
    @Override
    protected ChnlBizResponse callBizService(CupsFieldBean cupsFieldBean, CupsACRequest enchashmentRequest,
                                             EnchashmentResponse enchashmentResponse) throws Exception {
        //  请求体
        CupsField cupsField = cupsFieldBean.getCupsField();
        cupsField.setOrderAmt(AmountUtil.changeY2F(cupsField.getOrderAmt()));

        //  调用 CUPS   TODO 此交易配置暂时有误 ,关联了行内交易码, 后续解耦
        CupsFieldRspBean cupsFieldRspBean = callCups(cupsFieldBean);

        //  应答
        CupsField cupsFieldRsp = cupsFieldRspBean.getCupsFieldRsp();
        BeanUtil.beanCopy(cupsFieldRsp, enchashmentResponse);
        //  银联应答码 (原样)
        enchashmentResponse.setCupsRespCode(cupsFieldRsp.getRespCode());
        if (enchashmentResponse.getSettleDate() != null) {
            enchashmentResponse.setSettleDate(getCupsSettleDate(enchashmentResponse.getSettleDate()));
        }
        if (CupsParamConstant.SUCCESS_CODE.equals(cupsFieldRsp.getRespCode())) {
            enchashmentResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            enchashmentResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
        } else {
            if (!StringUtil.isEmptyOrNull(cupsFieldRsp.getAddData())) {
                F57 f57 = new F57();
                f57 = f57.parseF57_AS(cupsFieldRsp.getAddData());
                if (null != f57) {
                    enchashmentResponse.setAddDataRspCode(f57.getF57_AS_AR_2());
                }
            }
            enchashmentResponse.setRespCode(cupsFieldRsp.getRespCode());
            enchashmentResponse.setRespDesc(CupsRspCodeEnum.getRspMsg(cupsFieldRsp.getRespCode(), enchashmentResponse.getAddDataRspCode()));
        }
        return new ChnlBizResponse(enchashmentResponse);
    }
}
